package com.djmw.mouse.service.constants;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class ConstantValues<E extends Constants.CommonListType> extends ArrayList<E> {
	private static final long serialVersionUID = -5471953532330895226L;

	Map<Integer, E> entityMap = new HashMap<Integer, E>();

	@Override
	public boolean add(E e) {
		boolean x = super.add(e);
		entityMap.put(e.getId(), e);
		return x;
	}

	@Override
	public boolean remove(Object o) {
		boolean x = super.remove(o);
		entityMap.remove(((Constants.CommonListType) o).getId());
		return x;
	}

	@Override
	public void clear() {
		super.clear();
		entityMap.clear();
	}

	@Override
	public E set(int index, E element) {
		E e = super.set(index, element);
		entityMap.put(element.getId(), element);
		return e;
	}

	@Override
	public void add(int index, E element) {
		super.add(index, element);
		entityMap.put(element.getId(), element);
	}

	@Override
	public E remove(int index) {
		E e = super.remove(index);
		if (e != null) {
			entityMap.remove(e.getId());
		}
		return e;
	}
}
